package com.sgai.zero_robot.ui.alarm;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.sgai.zero_robot.base.BaseActivity;
import com.sgai.zero_robot.base.BaseViewModel;
import com.sgai.zero_robot.core.RobotManagerFactory;
import com.sgai.zero_robot.core.callback.AlarmCallback;
import com.sgai.zero_robot.databinding.ActivityAlarmBinding;
import com.sgai.zero_robot.pojo.AlarmInfo;
import com.sgai.zero_robot.ui.history.RunnableCallBack;
import com.sgai.zero_robot.utils.LogUtil;
import com.sgai.zero_robot.utils.ThreadPoolUtils;

import java.util.LinkedList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class AlarmActivity  extends BaseActivity<ActivityAlarmBinding, BaseViewModel> implements View.OnClickListener  {

    private long currentClickTime;

    @Override
    public void onClick(View v) {
        if (v == mBinding.ivCallback){
            finish();
        }
    }
    AlarmCallback alarmCallback = new AlarmCallback() {
        @Override
        public void onAlarm(AlarmInfo alarmInfo) {
            data.addFirst(alarmInfo);
        }

        @Override
        public void onInfo(String info) {

        }
    };
    private LinkedList<AlarmInfo> data = new LinkedList<>();
    LoadAlarmRunnable loadAlarmRunnable = new LoadAlarmRunnable(new RunnableCallBack<List<AlarmInfo>>() {
        @Override
        public void onResult(List<AlarmInfo> alarmInfos) {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    data.addAll(alarmInfos);
                    dismissLoading();
                    alarmListAdapter.setData(data);
                    RobotManagerFactory.getInstance().addAlarmCallback(alarmCallback);
                }
            });
        }
    });
    AlarmListAdapter alarmListAdapter = new AlarmListAdapter();
    @Override
    public void initViews(Bundle savedInstanceState) {
        mBinding.ivCallback.setOnClickListener(this);
        showLoading("正在加载...");
        ThreadPoolUtils.getInstance().addParallelTask(loadAlarmRunnable);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mBinding.rvAlarm.setLayoutManager(layoutManager);
        mBinding.rvAlarm.setAdapter(alarmListAdapter);
        mBinding.rvAlarm.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                Log.e(TAG, "onScrollStateChanged: "+newState);
                if (newState == 0){
                    currentClickTime = System.currentTimeMillis();
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
        startTimer();
    }
    private Timer timer;
    private TimerTask timerTask;

    private void startTimer(){
        stopTimer();
        timer = new Timer();
        timerTask = new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        LogUtil.e(TAG, "timer: " );
                        alarmListAdapter.setData(data);
                        if (data.size()>0&&(currentClickTime == 0||System.currentTimeMillis()-currentClickTime>5000)){
                            if (data!=null&&data.size()>0){
                                mBinding.rvAlarm.scrollToPosition(0);
                            }
                        }
                    }
                });
            }
        };
        timer.schedule(timerTask,1000,1000);
    }
    private void stopTimer(){
        if (timerTask!=null){
            timerTask.cancel();
            timerTask = null;
        }
        if (timer!=null){
            timer.cancel();;
            timer = null;
        }
    }
    @Override
    public void loadData() {

    }

    @Override
    protected void onPause() {
        super.onPause();
        RobotManagerFactory.getInstance().removeAlarmCallback(alarmCallback);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopTimer();
        ThreadPoolUtils.getInstance().removeParallelTask(loadAlarmRunnable);
    }
}